package com.recruit.g1.resume.service.impl;

import com.recruit.g1.common.response.JsonResult;
import com.recruit.g1.pojo.resume.dto.ResumeCreate;
import com.recruit.g1.pojo.resume.entity.Resume;
import com.recruit.g1.resume.mapper.ResumeMapper;
import com.recruit.g1.resume.service.ResumeService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class ResumeServiceImpl implements ResumeService {
    private static final Logger log = LogManager.getLogger(ResumeServiceImpl.class);
    @Autowired
    ResumeMapper resumeMapper;

    @Override
    public JsonResult create(ResumeCreate resumeCreate, @ApiIgnore HttpSession session) {
        Resume resume = new Resume();
        BeanUtils.copyProperties(resumeCreate,resume);

        /*User currentUser =
                (User) session.getAttribute(session.getId());

        if (currentUser==null){
            return JsonResult.fail(StateCode.NOT_LOGIN);
        }*/
        //resume.setUserId(currentUser.getId());

        log.debug("传递参数->  " + resume);

        resumeMapper.insert(resume);
        return JsonResult.ok("创建简历成功");
    }

    @Override
    public JsonResult selectById(Long id) {
        Resume resume = resumeMapper.selectById(id);
        if (resume==null){
            return JsonResult.fail("不存在");
        }
        return JsonResult.ok(resume);
    }


    @Override
    public Resume editById(Long id) {

        Resume resume =
        resumeMapper.selectById(id);

        if (resume==null){
            throw new RuntimeException("没有找到所选简历");
        }

        return resume;

    }

    @Override
    public JsonResult deleteById(Long id) {

        int result = resumeMapper.deleteById(id);

        if (result==1){
            return JsonResult.ok(result);
        }
        return JsonResult.fail("删除失败");
    }

    @Override
    public JsonResult selectByUserId(Long id) {

        List<Resume> resumes =
        resumeMapper.selectByUserId(id);

        return JsonResult.ok(resumes);
    }
}
